<% @reports = @reports.includes(:user) %>

<%= index_for @reports, class: 'table table-striped table-hover' do |i| %>
  <%= i.attribute :_check, sortable: false do |report| %>
    <span class="glyphicon glyphicon-select" data-report-id="<%= report.id %>"></span>
  <% end %>

  <%= i.attribute :publish_at, format: :date %>
  <%= i.fields_for :user, model: Ssp::User do |ii| %>
    <%= ii.attribute :nickname %>
    <%= ii.attribute :sstype do |user| %>
      <%= Ssp::User::SSTYPES.key user.sstype %>
    <% end %>
  <% end if current_user.admin %>
  <%= i.attribute :items_count %>
  <%= i.attribute :price_sum %>
  <%= i.attribute :count_sum %>
  <%= i.attribute :created_at, format: :long %>
  <%= i.attribute :reported_at, format: :long if params[:status] == 'reported' %>
  <%= i.attribute :status do |report| %>
    <%= Ssp::Report::STATUS.key report.status.to_sym %>
  <% end unless params[:status] %>

  <%= i.actions do |a| %>
    <%= a.action_link :edit unless i.object.status == 'reported' %>
    <%= a.action_link :show if i.object.status == 'reported' %>
    <% if current_user.admin %>
      <%= a.action_link :confirm, method: :post if i.object.status == 'unconfirmed' %>
      <%= a.action_link :reject, method: :post if i.object.status == 'confirmed' %>
      <%= a.action_link :destroy, method: :delete, confirm: '删除日报后无法恢复，且已同步日报需要您在商务部日报系统中手动删除，请谨慎处理，您确认要删除这条日报？' %>
    <% end %>
  <% end %>
<% end %>

<div>订单数累计 <%= @reports.map(&:count_sum).sum %> 笔，订单金额累计 <%= (@reports.map(&:price_sum).sum.to_f / 10000).round(2) %> 万元</div>
<div class="select-report-info"></div>

<script type="text/javascript">
  var actions = <% if current_user.admin %>"，您可以全部 <a href=\"<%= ssp_reports_path %>/batch/confirm\" data-action=\"confirm\" data-confirm=\"您确认要通过审核当前选中的所有日报？\">通过审核</a> <a href=\"<%= ssp_reports_path %>/batch/reject\" data-action=\"reject\" data-confirm=\"您确认要重新审核当前选中的所有日报？\">重新审核</a> <a href=\"<%= ssp_reports_path %>/batch/destroy\" data-action=\"destroy\" data-confirm=\"删除日报后无法恢复，且已同步日报需要您在商务部日报系统中手动删除，请谨慎处理，您确认要当前选中的所有日报？\">删除</a>"<% else %>""<% end %>;

  $('#ssp_reports:not(.initialized)')
    .addClass('initialized')
    .DataTable({
      order: [],
      pagingType: 'numbers',
      columnDefs: [{
        orderable: false,
        targets: 0
      }],
      select: true,
      language: {
        select: {
          rows: {
            0: "，按住 <code>ctrl</code> 或 <code>shift</code> 可以多选",
            1: "，当前选中了 1 条数据" + actions,
            _: "，当前选中了 %d 条数据" + actions,
          }
        }
      }
    });

  var table = $('#ssp_reports').DataTable();
  function updateBatchActions() {
    var selects = table.rows({ selected: true });
    if (selects.nodes().length > 0) {
      var ids = selects.nodes().map(function(node) { return $(node).find('.glyphicon').data('report-id'); });
      $('.select-info a[data-action]').each(function() {
        var $this = $(this);
        $this.attr('href', '<%= ssp_reports_path %>/batch/' + ids.join(',') + '/' + $this.data('action'));
      });

      var data = selects.data();
      $('.select-report-info').html('当前选中 ' + ids.length + ' 条日报，订单数累计 ' + data.reduce(function(s, d) { return s + parseInt(d[5]); }, 0) + ' 笔，订单金额累计 ' + (data.reduce(function(s, d) { return s + parseInt(d[4]); }, 0) / 10000).toFixed(2) + ' 万元');
    } else {
      $('.select-report-info').html('');
    }
  }

  table.on('select', updateBatchActions);
  table.on('deselect', updateBatchActions);
  table.rows({ selected: false }).nodes().each(function(node) {
    $(node).removeClass('selected')
  });
</script>

<style>
  #ssp_reports_wrapper > .row > .col-sm-7 {
    pointer-events: none;
  }
  #ssp_reports_wrapper > .row > .col-sm-7 .pagination {
    pointer-events: all;
  }
</style>
